package com.cykj.enterprise.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSON;
import com.cykj.common.dto.ResponseDTO;
import com.cykj.common.entity.CareerExpectDO;
import com.cykj.common.entity.JobDO;
import com.cykj.common.entity.RecruitInfoDO;
import com.cykj.common.entity.RegionDO;
import com.cykj.enterprise.mapper.HomeMapper;
import com.cykj.enterprise.service.IHomeService;
import com.cykj.enterprise.vo.CareerExpectVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @BelongsProject: smart_recruit_backend
 * @BelongsPackage: com.cykj.enterprise.service.impl
 * @Author: 小白糖糖
 * @CreateTime: 2025-08-15 10:32
 * @Description: TODO
 * @Version: 1.0
 */
@Service
public class HomeServiceImpl implements IHomeService {
    @Autowired
    private HomeMapper homeMapper;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Value("${fileUploadPath.httpPath}")
    private String httpPath;

    @Override
    public ResponseDTO getRecommendationList(CareerExpectVO careerExpectVO) {
        PageInfo<Object> page;
        List<CareerExpectDO> recommendationList;

        PageHelper.startPage(careerExpectVO.getPageNum(), careerExpectVO.getPageSize());
        recommendationList = homeMapper.getRecommendationList(careerExpectVO);

        recommendationList = getFullRegionName(recommendationList);
        page = new PageInfo<>(recommendationList);

        if (page.getTotal() != 0 && recommendationList.isEmpty()) {
            careerExpectVO.setPageNum(1);
            PageHelper.startPage(careerExpectVO.getPageNum(), careerExpectVO.getPageSize());
            recommendationList = homeMapper.getRecommendationList(careerExpectVO);
            recommendationList = getFullRegionName(recommendationList);
            page = new PageInfo<>(recommendationList);
        }
        return ResponseDTO.success(page);
    }

    @Override
    public ResponseDTO getRecruitList() {
        Integer hrId = StpUtil.getLoginIdAsInt();
        System.out.println(hrId);
        List<JobDO> recruitInfoByHrId = homeMapper.getRecruitInfoByHrId(hrId);
        return ResponseDTO.success(recruitInfoByHrId);
    }

    @Override
    public void getAllRegion() {
        List<RegionDO> allRegion = homeMapper.getAllRegion();
        for (RegionDO item : allRegion) {
            redisTemplate.opsForValue().set("region:" + item.getRegionId(), item);
        }
    }


    private List<CareerExpectDO> getFullRegionName(List<CareerExpectDO> list) {
        for (CareerExpectDO item : list) {
            RegionDO regionDO = item.getRegionDO();
            RegionDO childrenRegion;
            RegionDO parentRegion;
            String headIcon = item.getUserDO().getHeadIcon();
            if (headIcon != null){
                headIcon = httpPath + headIcon;
                item.getUserDO().setHeadIcon(headIcon);
            }
            try {
                Object childrenObj = redisTemplate.opsForValue().get("region:" + regionDO.getRegionId());
                childrenRegion = JSON.parseObject(childrenObj.toString(), RegionDO.class);
                Object parentObj = redisTemplate.opsForValue().get("region:" + regionDO.getParentId());
                parentRegion = JSON.parseObject(parentObj.toString(), RegionDO.class);
            } catch (Exception e) {
                getAllRegion();
                childrenRegion = item.getRegionDO();
                parentRegion = homeMapper.getRegionDOById(item.getRegionDO().getParentId());
            }
            String fullName = parentRegion.getRegionName() + childrenRegion.getRegionName();
            item.getRegionDO().setRegionName(fullName);


        }
        return list;
    }
}
